<?php
/**
 * OpenPASL
 *
 * Copyright (c) 2008, Danny Graham, Scott Thundercloud
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *   * Neither the name of the Danny Graham, Scott Thundercloud, nor the names of
 *     their contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @copyright Copyright (c) 2008, Danny Graham, Scott Thundercloud
 */
namespace PASL\Data\Validation;

require_once('PASL/Data/Validation/Validation.php');
require_once('PASL/Log.php');
require_once('PASL/Data/Validation/iValidator.php');
require_once('PASL/Data/Validation/Error.php');



use PASL\Log;
use PASL\Data\Validation;
use PASL\Data\Validation\Error;
use PASL\Data\Validation\iValidator;

/**
 * Class to validate an data in an array.
 * 
 * @package PASL_Data_Validation
 * @category Data Validation
 * @author Scott Thundercloud <scott.tc@gmail.com>
 */
class DArray extends Validation implements iValidator
{
	private $Callback = Array();
	private $GlobalCallback = null;


	/**
	 * Set a callback function for a specific key
	 * 
	 * @param string $Key
	 * @param string $Callback
	 * @return void
	 */
	public function addCallback($Key, $Callback)
	{
		$this->Callback[$Key] = $Callback;
	}


	/**
	 * Set a global callback function for each item
	 * 
	 * @param string|array $Callback
	 * @return void
	 */
	public function setGlobalCallback($Callback)
	{
		$this->GlobalCallback = $Callback;
	}

	/**
	 * Validate the items in the array
	 * 
	 * @return boolean
	 */
	public function Validate()
	{
		$Data = $this->getData();
		
		foreach($Data AS $key=>$val)
		{
			$Callback = (!empty($this->Callback[$key])) ? $this->Callback[$key] : ($this->GlobalCallback !== null) ? $this->GlobalCallback : false;
			if($Callback === false)
			{
				Log::Add(__CLASS__.': Callback not set for "{$key}"');
				continue;
			}
			
			if(is_array($Callback))
			{
				$obj = $Callback[0];
				$method = $Callback[1];

				// Method
				if(!is_object($obj))
				{
					$Object = new \ReflectionClass($obj);
					$obj = $Object->newInstance();
				} else $Object = new \ReflectionObject($obj); // Object

				$Method = $Object->getMethod($method);
				$Data = $Method->invoke($obj, $key, $val);
			}
			else
			{
				
				// Run function
				$Function = new \ReflectionFunction($Callback);
				$Data = $Function->invoke($key, $val);
			}

			if($Data !== true)
			{
				$Error = new Error;
				$Error->Message = $Data;
				$Error->Name = $key;
				$Error->Value = $val;

				$this->addError($Error);
			}
		}

		if(count($this->getErrors()) == 0)
		{
			$this->setValidated(true);
			return true;
		}
		else
		{
			$this->setValidated(false);
			return false;
		}
	}
}
?>